package mspbots.qbd.desktop.core.profitandloss;

import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.extern.log4j.Log4j2;
import mspbots.qbd.common.RabbitKeys;
import mspbots.qbd.desktop.core.BaseGenericService;
import mspbots.qbd.desktop.utils.XmlResponseUtils;
import org.springframework.stereotype.Service;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import reactor.core.publisher.Flux;

/**
 * mspbots.data.qbd.ws.profitandloss.ProfitAndLossService
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2020/6/4
 */
@Log4j2
@Service
public class ProfitAndLossService extends BaseGenericService {

    public void sendYTDComp(NodeList nodeList, Long tenantId) {
        for (int temp = 0; temp < nodeList.getLength(); temp++) {
            Node node = nodeList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) node;
                NodeList reportData = eElement.getElementsByTagName("ReportData");
                for (int x = 0; x < reportData.getLength(); x++) {
                    ArrayNode arrayNode = XmlResponseUtils.visitChildNodes(reportData.item(x),
                            eElement.getElementsByTagName("ReportSubtitle").item(0).getTextContent(), tenantId);
                    Flux.fromIterable(arrayNode)
                            .subscribe(objectNode -> this.messagingTemplate
                                    .convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME,
                                            RabbitKeys.PROFITANDLOSS_YTDCOMP_SYNC_QUEUE_KEY, objectNode));
                }
            }
        }
    }

    public void sendPrevYearComp(NodeList nodeList, Long tenantId) {
        for (int temp = 0; temp < nodeList.getLength(); temp++) {
            Node node = nodeList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) node;
                NodeList reportData = eElement.getElementsByTagName("ReportData");
                for (int x = 0; x < reportData.getLength(); x++) {
                    ArrayNode arrayNode = XmlResponseUtils.visitChildNodes(reportData.item(x),
                            eElement.getElementsByTagName("ReportSubtitle").item(0).getTextContent(), tenantId);
                    Flux.fromIterable(arrayNode)
                            .subscribe(objectNode -> this.messagingTemplate
                                    .convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME,
                                            RabbitKeys.PROFITANDLOSS_PREVYEARCOMP_SYNC_QUEUE_KEY,
                                            objectNode));
                }
            }
        }
    }

    public void sendStandard(NodeList nodeList, Long tenantId) {
        for (int temp = 0; temp < nodeList.getLength(); temp++) {
            Node node = nodeList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) node;
                NodeList reportData = eElement.getElementsByTagName("ReportData");
                for (int x = 0; x < reportData.getLength(); x++) {
                    ArrayNode arrayNode = XmlResponseUtils.visitChildNodes(reportData.item(x),
                            eElement.getElementsByTagName("ReportSubtitle").item(0).getTextContent(), tenantId);
                    Flux.fromIterable(arrayNode)
                            .subscribe(objectNode -> this.messagingTemplate
                                    .convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME,
                                            RabbitKeys.PROFITANDLOSS_STANDARD_SYNC_QUEUE_KEY,
                                            objectNode));
                }
            }
        }
    }

    public void sendDetail(NodeList nodeList, Long tenantId) {
        for (int temp = 0; temp < nodeList.getLength(); temp++) {
            Node node = nodeList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) node;
                NodeList reportData = eElement.getElementsByTagName("ReportData");
                for (int x = 0; x < reportData.getLength(); x++) {
                    ArrayNode arrayNode = XmlResponseUtils.visitChildNodes(reportData.item(x),
                            eElement.getElementsByTagName("ReportSubtitle").item(0).getTextContent(), tenantId);
                    Flux.fromIterable(arrayNode)
                            .subscribe(objectNode -> this.messagingTemplate
                                    .convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME,
                                            RabbitKeys.PROFITANDLOSS_DETAIL_SYNC_QUEUE_KEY,
                                            objectNode));
                }
            }
        }
    }

}
